<script>
import {apiSendComment} from "@/api/api";

export default {
  props:{
    productId: {
      type: Number,
      default : () => null
    }
  },
  name: "Common",
  data(){
    return{
      comment: {
        star : 5,
        productId: null,
        content : ""
      },
      rules: {
        content: [
          { required: true, message: '请输入邮箱', trigger: 'change' },
        ],
      }
    }
  },
  methods:{
    async submitForm() {
      this.comment.productId = this.productId;
      const promise = await apiSendComment(this.comment);
      if(promise.code === 200){
        this.$message.success("发表成功");
      }else {
        this.$message.error(promise.message);
      }
    }
  }
}
</script>

<template>
  <div class="common-bg">
    <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="comment"
        :rules="rules"
    >
      <el-form-item label="评分" prop="name" style="display: flex;width: 100%">
        <div class="slider-demo-block">
          <el-slider v-model="comment.star" :step="1" :max="5" show-stops/>
        </div>
      </el-form-item>
      <el-form-item label="评价" prop="desc">
        <el-input
            v-model="comment.content"
            style="width: 500px"
            :autosize="{ minRows: 2, maxRows: 4 }"
            type="textarea"
            placeholder="输入评论"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          Submit
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped lang="less">
.common-bg {
  width: 100%;
}

::v-deep .el-form-item__content{
  flex: 1;
}
.slider-demo-block{
  width: 90%;
}
</style>